package com.sigmatrix.h5.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sigmatrix.h5.dao.InspectionRecordsDao;
import com.sigmatrix.h5.entity.AdminArea;
import com.sigmatrix.h5.entity.DisDealer;
import com.sigmatrix.h5.entity.FirstLevelQRCode;
import com.sigmatrix.h5.entity.InspectionRecords;
import com.sigmatrix.h5.entity.InspectionRecordsDetail;
import com.sigmatrix.h5.entity.MultistageLevelQRCode;
import com.sigmatrix.h5.enums.InsType;
import com.sigmatrix.h5.service.AdminAreaService;
import com.sigmatrix.h5.service.DisDealerService;
import com.sigmatrix.h5.service.FirstLevelQrCodeService;
import com.sigmatrix.h5.service.InspectionRecordsDetailService;
import com.sigmatrix.h5.service.InspectionRecordsService;
import com.sigmatrix.h5.service.MultistageLevelQrCodeService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.SessionUtils;
import com.sigmatrix.h5.web.args.InsParam;
import com.sigmatrix.h5.web.args.SaveInsParam;
import com.sigmatrix.h5.web.mode.Inspection;
import com.sigmatrix.sc.jdbc.common.lang.Page;
import com.sigmatrix.sc.jdbc.common.service.ServiceImpl;
/**
 * 
 *@ClassName: InspectionRecordsServiceImpl
 *@Description: 
 *@author MH
 *@date 2017年9月21日 下午3:20:03
 */
@Service
public class InspectionRecordsServiceImpl extends ServiceImpl<InspectionRecords,Long> implements InspectionRecordsService{
	
	@Autowired
	InspectionRecordsDao inspectionRecordsDao;
	
	@Autowired
	FirstLevelQrCodeService firstLevelQrCodeService;
	
	@Autowired
	MultistageLevelQrCodeService multistageLevelQrCodeService;
	
	@Autowired
	InspectionRecordsDetailService inspectionRecordsDetailService;
	
	@Autowired
	DisDealerService distributionDealersService;
	
	@Autowired
	AdminAreaService adminAreaService;
	
	@Override
	@Transactional
	public Long save(SaveInsParam vo,String remoutIp){
		//验证必要的参数
		validateSave(vo);
		vo.setQrCode(vo.getQrCode());
		//判断是一级码表还是多级码表
		FirstLevelQRCode fc = null;
		MultistageLevelQRCode mc = null;
		boolean f = true;
		if(vo.getPackLevel().equals(1)){
			fc = firstLevelQrCodeService.findByQRCode(vo.getQrCode());
		}else{
			mc = multistageLevelQrCodeService.findByPackContent(vo.getQrCode());
			f = false;
		}
		Assert.leastOneValue(fc, mc, "qrCode错误");
		InspectionRecords ir = new InspectionRecords();
		InspectionRecordsDetail ird = new InspectionRecordsDetail();
		ir.setAccount(SessionUtils.getAccount());
		ir.setQrCode(vo.getQrCode());
		ir.setStatus(vo.getStatus());
		ir.setUserId(SessionUtils.getUserId());
		ir.setProductId(f?fc.getProductId():mc.getProductId());
		ir.setCreateTime(new Date());
		Long insId = inspectionRecordsDao.insertSelective(ir);
		BeanUtils.copyProperties(vo, ird);
		ird.setType(StringUtils.isBlank(vo.getVisualCode())?InsType.SCAN.value:InsType.SCAN.value);//稽查方式 1扫码稽查，2输码稽查
		ird.setBatchCode(f?fc.getBatchCode():mc.getBatchCode());
		ird.setOutStorageTime(f?fc.getOutStockTime():mc.getOutStockTime());
		ird.setProductTime(f?fc.getProductTime():mc.getProductTime());
		ird.setCodeLevel(vo.getPackLevel());
		ird.setInspectionRecordsId(insId);
		ird.setScanIp(remoutIp);
		ird.setWrapName(vo.getPackName());
		ird.setInspectionStatus(vo.getStatus());
		ird.setSeqNum((f?fc.getSeqNum():mc.getSeqNum()));
		inspectionRecordsDetailService.insertSelective(ird);
		return insId;
	}
	/**
	 * 
	 *@Title: validateSave
	 *@Description: 保存时验证必要参数
	 *@return void
	 *@author MH
	 *@date 2017年10月20日 下午10:53:59
	 *@param vo
	 */
	private void validateSave(SaveInsParam vo){
		Assert.notNull(vo.getQrCode(), "qrCode不能为空");
		Assert.notNull(vo.getPackLevel(), "packLevel不能为空");
		Assert.notNull(vo.getCodeFirstDealerId(), "codeFirstDealerId不能为空");
		//Assert.notNull(vo.getDealerInspectionStatus(), "dealerInspectionStatus不能为空");
		Assert.notNull(vo.getAreaInspectionStatus(), "AreaInspectionStatus不能为空");
		Assert.leastOneValue(vo.getScanCityId(), vo.getInspectionCityId(), "scanCityId和inspectionCityId不能同时为空");
		Assert.notNull(vo.getStatus(), "status不能为空");
	}
	@Override
	public Inspection detail(Long id) {
		Assert.notNull(id, "id不能为空");
		Inspection ins = inspectionRecordsDao.detail(id);
		Assert.notNull(ins, "没有查询到记录");
		convertInspection(ins);
		return ins;
	}

	@Override
	public Page<Inspection> list(InsParam vo, int pageNo, int pageSize) {
		Page<Inspection> page = inspectionRecordsDao.findByCondition(vo, pageNo, pageSize);
//		if(CommonUtils.isNotEmpty(page)){
//			for (Inspection ins : page) {
//				convertInspection(ins);
//			}
//		}
		return page; 
	}
	
	private void convertInspection(Inspection ins){
		//查询经销商名称
		List<DisDealer> dealers = distributionDealersService.findByIds(ins.getInspectionDealerId(),
				ins.getCodeFirstDealerId());
		if(CommonUtils.isNotEmpty(dealers)){
			dealers.stream().anyMatch(x ->{
				if(CommonUtils.isNotEmpty(ins.getCodeFirstDealerName()) 
						&& CommonUtils.isNotEmpty(ins.getInspectionDealerName()))
						return true;
				if(x.getId().equals(ins.getCodeFirstDealerId())){
					ins.setCodeFirstDealerName(x.getName());
				}
				if(x.getId().equals(ins.getInspectionDealerId())){
					ins.setInspectionDealerName(x.getName());
				}
				return false;
			});
		}
		//查询地点
		List<AdminArea> areas = adminAreaService.findByIds(ins.getInspectionCityId(),ins.getInspectionProvinceId(),
				ins.getScanCityId(),ins.getScanProvinceId());
		if(CommonUtils.isNotEmpty(areas)){
			for (AdminArea area : areas) {
				if(area.getId().equals(ins.getInspectionCityId())){
					ins.setInspectionCityName(area.getName());
				}
				if(area.getId().equals(ins.getInspectionProvinceId())){
					ins.setInspectionProvinceName(area.getName());
				}
				if(area.getId().equals(ins.getScanCityId())){
					ins.setScanCityName(area.getName());
				}
				if(area.getId().equals(ins.getScanProvinceId())){
					ins.setScanProvinceName(area.getName());
				}
			}
		}
	}
}
